<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\bill;

use app\common\dao\BaseDao;
use app\common\model\bill\BillFinanceModel;
use app\common\model\property\cost\PropertyCostFinanceModel;
use app\common\repositories\bill\BillCateRepository;
use app\common\repositories\property\cost\PropertyCostCateRepository;
use think\db\BaseQuery;

class BillFinanceDao extends BaseDao
{

    /**
     * @return BillFinanceModel
     */
    protected function getModel(): string
    {
        return BillFinanceModel::class;
    }


    /**
     * @param array $where
     * @return BaseQuery
     */
    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['cate_id']) && $where['cate_id'] !== '', function ($query) use ($where) {
                $query->where('cate_id', (int)$where['cate_id']);
            })
            ->when(isset($where['cates_id']) && $where['cates_id'] !== '', function ($query) use ($where) {
                /** @var BillCateRepository $billCateRepository */
                $billCateRepository = app()->make(BillCateRepository::class);
                $query->where('cate_id', 'in', array_merge([$where['cates_id']], $billCateRepository->getChildIds($where['cates_id'])));
            })
            ->when(isset($where['contract_id']) && $where['contract_id'] !== '', function ($query) use ($where) {
                $query->where('contract_id', (int)$where['contract_id']);
            })
            ->when(isset($where['landlord_id']) && $where['landlord_id'] !== '', function ($query) use ($where) {
                $query->where('landlord_id', (int)$where['landlord_id']);
            })
            ->when(isset($where['room_id']) && $where['room_id'] !== '', function ($query) use ($where) {
                $query->where('room_id', (int)$where['room_id']);
            })
            ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use ($where) {
                $query->where('is_type', (int)$where['is_type']);
            })
            ->when(isset($where['pay_mode']) && $where['pay_mode'] !== '', function ($query) use ($where) {
                $query->where('pay_mode', (int)$where['pay_mode']);
            })
            ->when(isset($where['catalog_id']) && $where['catalog_id'] !== '', function ($query) use ($where) {
                $query->where('catalog_id', (int)$where['catalog_id']);
            })
            ->when(isset($where['pay_type']) && $where['pay_type'] !== '', function ($query) use ($where) {
                $query->where('pay_type', (int)$where['pay_type']);
            })
            ->when(isset($where['finance_type']) && $where['finance_type'] !== '', function ($query) use ($where) {
                $query->where('finance_type', (int)$where['finance_type']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->where('remark|finance_no', 'like', '%' . trim($where['keywords']) . '%');
            })
            ->when(isset($where['add_time']) && $where['add_time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['add_time'], 'add_time');
            });
    }

}
